package org.ru.mse10.cvis.service;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * The Class EntityServiceStateless.
 */
@Stateless
public class EntityServiceStateless {

	/** The entity manager. */
	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * Gets the entity manager.
	 * 
	 * @return the entity manager
	 */
	protected EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Find.
	 * 
	 * @param <E>
	 *            the element type
	 * @param clazz
	 *            the clazz
	 * @param id
	 *            the id
	 * @return the e
	 */
	public <E> E find(Class<E> clazz, Long id) {
		return getEntityManager().find(clazz, id);
	}

	/**
	 * Save.
	 * 
	 * @param <E>
	 *            the element type
	 * @param e
	 *            the e
	 * @return the e
	 */
	public <E> E save(E e) {
		return getEntityManager().merge(e);
	}

	/**
	 * Save in new tx.
	 * 
	 * @param <E>
	 *            the element type
	 * @param e
	 *            the e
	 * @return the e
	 */
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public <E> E saveInNewTx(E e) {
		return save(e);
	}
}
